<?php

/*
 *
 * 功能：
 */

namespace Center\Controller;

use Think\C_Controller;

class QuestionnaireController extends C_Controller  {

	public function __construct() {
        parent::__construct();
        /*$store_id = (int)$_GET['store_id'];
        if($store_id<1){
            exit;
        }*/
        $this->q = new \Home\Logic\QuestionnaireLogic();

    }

    public function suggest(){
    	layout(false);
    	$user_id = $_SESSION['client_id'];
        if($user_id>1){
            $b = new \Home\Logic\UserLogic();
            $res = $b->getUserById($user_id);
            $user = $res['mobile'];
        }
        
    	$re = $this->q->getFeedback();
    	$this->assign('feedback',$re);
    	$this->assign('user',$user);
    	$this->display('suggest');
    }

    // 用户反馈 登录
    public function suggest_login(){
        $mobile = trim($_GET['mobile']);
        $code = trim($_GET['code']);
        import('MsgCode','Msgcode');
        $m = new \Msgcode\MsgCode();
        if(!$m->checkCode($mobile, $code)){
            echo 'false';
            exit;
        }
        $b = new \Home\Logic\UserLogic();
        $user = $b->getUserByAccount($mobile);
        if($user['id']<1){
            $user['id'] = $b->createUser($mobile);
        }
        //设置登录状态
        $_SESSION['client_id'] = $user['id'];
        $this->setSession($_SESSION);
        import('Logs', 'Log');
        $mongo = new \Log\Logs();
        $mongo->login_Log($user['id'],2);
        
        echo $mobile;
        exit;
    }


    // 反馈 提交
    public function submit_suggetion(){
    	$mobile= $_GET['mobile'];
    	$b = new \Home\Logic\UserLogic();
        $user = $b->getUserByAccount($mobile);
        if($user){
        	$data['user_id'] = $user['id'];
        }else{
        	echo 'false';
        	exit;
        }
    	$data['create_time'] = time();
    	$daan = $_GET['daan'];
    	$text = $_GET['content'];
    	if(strlen($text)<1 ){
    		echo 'false';
    		exit;
    	}
    	$arr = array($daan=>$text);
    	$data['content'] = json_encode($arr);
    	$re = $this->q->insertUser_suggestion($data);
    	if($re){
    		echo 'true';
    		exit;
    	}else{
    		echo 'false';
    		exit;
    	}
    	exit;
    }

    // 调查问卷
    public function questions(){
        layout(false);
        $questions = $this->q->questions();
        $user = $_SESSION['msg_mobile'];
        $this->assign('questions',$questions);
        $this->assign('user',$user); 
        $this->display('questionnaire');
    }

    // 调查问卷 提交
    public function submit_questions(){
        $mobile = $_GET['user'];
        
        $b = new \Home\Logic\UserLogic();
        $user = $b->getUserByAccount($mobile);
        if($user){
            $data['user_id'] = $user['id'];
        }else{
            echo 'false';
            exit;
        }
        $data['create_time'] = time();
        $data['answer'] = json_encode($_POST);
        $re = $this->q->insertUser_questions($data);
        if($re){
            echo 'true';
            exit;
        }else{
            echo 'false';
            exit;
        }
        exit;
    }
	
}